{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入所需要的库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import math\n",
    "import datetime\n",
    "import collections"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 对文件进行读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"gaze.csv\",encoding='unicode_escape') as fp:\n",
    "    num_cof=0#统计可信度超过0.9的个数\n",
    "    num_pos_x=[]#储存坐标值\n",
    "    num_pos_y=[]\n",
    "    time=[]#储存时间戳\n",
    "    samp = collections.defaultdict(lambda: 0) #对于数据采样率的统计\n",
    "    reader = csv.DictReader(fp)\n",
    "    for i in reader:\n",
    "        if float(i[\"confidence\"])>=0.9:\n",
    "            num_cof+=1\n",
    "        num_pos_x.append(i[\"norm_pos_x\"])\n",
    "        num_pos_y.append(i[\"norm_pos_y\"])\n",
    "        time.append(datetime.datetime.fromtimestamp(float(i[\"gaze_timestamp\"])))\n",
    "        samp[i[\"gaze_timestamp\"].split(\".\")[0]]+=1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算坐标值的平均数方差等数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "    num_pos_x=list(map(float,num_pos_x))\n",
    "    num_pos_y=list(map(float,num_pos_y))\n",
    "    avg_x=sum(num_pos_x)/len(num_pos_x)\n",
    "    avg_y=sum(num_pos_y)/len(num_pos_y)#平均数\n",
    "    fin_x=0\n",
    "    fin_y=0\n",
    "    for i in num_pos_x:\n",
    "        fin_x+=(i-avg_x)**2\n",
    "    for i in num_pos_y:\n",
    "        fin_y+=(i-avg_y)**2\n",
    "    s_x=math.sqrt(fin_x/len(num_pos_x))#x轴的方差\n",
    "    s_y=math.sqrt(fin_y/len(num_pos_y))#y轴的方差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 统计符合3sigma原则的个数 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "    number_pos=0\n",
    "    for i in range(len(num_pos_x)):\n",
    "        if avg_x-(3*s_x)<=num_pos_x[i]<=avg_x+(3*s_x) and avg_y-(3*s_y)<=num_pos_y[i]<=avg_y+(3*s_y):\n",
    "            number_pos+=1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据得到的数据个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87442\n",
      "120036\n",
      "1970-01-02 08:29:10.776780\n",
      "426\n"
     ]
    }
   ],
   "source": [
    "    print(num_cof)#可信度超过0.9的个数\n",
    "    print(number_pos)#符合3sigma原则的个数 \n",
    "    print(time[0])#储存了改变后的时间戳，样例输出第一个\n",
    "    print(int(sum(list(dict(samp).values()))/len(list(dict(samp).values()))))#samp储存了数据的采样率，这里输出了采样率的均值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 按照采样率100hz统计的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['88150.77678', '0', '0.8280059462620806', '0.5314327126985153', '0.39750946211500204', '88150.77678-0', '28.369752901573236', '52.033134560158175', '705.1211159195539', '20.0', '15.0', '-20.0', '0.01152676874215287', '0.051001789765111646', '0.9986320398640939', '', '', '', '', '', ''], ['88150.776787', '0', '0.4597050219822415', '0.3370509818852456', '0.18614705187838343', '88150.776787-1', '-142.8698656268901', '154.78812076917725', '684.9816207079147', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14176132023816898', '0.19251814742259893', '0.9709997378976485', '', '', '', '', '', ''], ['88150.779709', '0', '0.888789360206567', '0.5431396650463463', '0.4106679784529125', '88150.779709-0', '38.9420506661925', '45.3598633490285', '705.2321117996576', '20.0', '15.0', '-20.0', '0.02608686780828404', '0.04181140446832481', '0.9987849026613991', '', '', '', '', '', ''], ['88150.779759', '0', '0.5502584923810523', '0.33792335450596644', '0.17330059543683884', '88150.779759-1', '-141.8791689729382', '160.8677791527232', '683.8931076235057', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14039693931103547', '0.2008910130307643', '0.9695006448247298', '', '', '', '', '', ''], ['88150.787485', '1', '0.5025948800258426', '0.3401089118572799', '0.14298949506249015', '88150.787485-1', '-139.4048601388959', '175.088321362755', '681.1514528333081', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.13698933743054134', '0.2204754503935745', '0.9657248558487515', '', '', '', '', '', ''], ['88150.78757999999', '1', '0.6251340345888996', '0.48413892950590753', '0.3854906757379729', '88150.78368299999-0 88150.79147699999-1', '6.784204169255643', '-27.55051951844103', '-334.1615251760621', '20.0', '15.0', '-20.0', '0.041430064528985594', '0.03483702678214182', '0.9985338909211371', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14508725538827758', '0.23074439357001145', '0.9621365356122304'], ['88150.7894805', '1', '0.6251340345888996', '0.48968686416117124', '0.3928486879224061', '88150.787484-0 88150.79147699999-1', '4.18043690634285', '-24.431546325680692', '-316.6802400472673', '20.0', '15.0', '-20.0', '0.05249507001573374', '0.02982528943144682', '0.9981756958243246', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14508725538827758', '0.23074439357001145', '0.9621365356122304'], ['88150.7935835', '1', '0.6251340345888996', '0.4959927312055917', '0.39902068406553226', '88150.79569-0 88150.79147699999-1', '1.5330999491406576', '-21.730861601028412', '-298.89044052583546', '20.0', '15.0', '-20.0', '0.06514516047431963', '0.027374145920454677', '0.9975002577452808', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14508725538827758', '0.23074439357001145', '0.9621365356122304'], ['88150.7956825', '1', '0.7063552247599557', '0.49397474725559637', '0.4084287570104117', '88150.79569-0 88150.795675-1', '2.226757768794034', '-19.036159854780287', '-288.72722534107356', '20.0', '15.0', '-20.0', '0.06514516047431963', '0.027374145920454677', '0.9975002577452808', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1530847520875171', '0.2209875356663993', '0.9631871924804625'], ['88150.79840649999', '1', '0.7084760742125342', '0.4941157090777496', '0.4071094106264913', '88150.79569-0 88150.801123-1', '2.1796287066650546', '-19.354529932692056', '-289.3865972267117', '20.0', '15.0', '-20.0', '0.06514516047431963', '0.027374145920454677', '0.9975002577452808', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1525226416003532', '0.22265933259715345', '0.9628913050841411'], ['88150.80114', '1', '0.7084760742125342', '0.49514541550942315', '0.40833847319653394', '88150.801157-0 88150.801123-1', '1.7824437847763495', '-18.930992853762366', '-286.84930905199883', '20.0', '15.0', '-20.0', '0.06722208090235383', '0.026625007974036028', '0.9973827253314249', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1525226416003532', '0.22265933259715345', '0.9628913050841411'], ['88150.80297349999', '1', '0.8396615664739172', '0.4952159765740955', '0.4034417973020016', '88150.801157-0 88150.80479-1', '1.7573657633000934', '-19.951730221676957', '-286.9847992683296', '20.0', '15.0', '-20.0', '0.06722208090235383', '0.026625007974036028', '0.9973827253314249', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15222844217062562', '0.22981420200416583', '0.9612553947580701'], ['88150.8046305', '1', '0.7910322078633543', '0.49472831656855015', '0.40514624513285424', '88150.804471-0 88150.80479-1', '1.9446636659802365', '-19.682136143449362', '-288.19418100551803', '20.0', '15.0', '-20.0', '0.06623886767176704', '0.023517941823706083', '0.9975266005585715', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15222844217062562', '0.22981420200416583', '0.9612553947580701'], ['88150.80726949999', '1', '0.8563407607235008', '0.49477736411660694', '0.4049685390557781', '88150.809749-0 88150.80479-1', '1.925754580585762', '-19.710658057686643', '-288.0721152448382', '20.0', '15.0', '-20.0', '0.066337739085217', '0.02383990799003523', '0.9975123874719999', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15222844217062562', '0.22981420200416583', '0.9612553947580701'], ['88150.8097485', '1', '0.8563407607235008', '0.4948883596911874', '0.4022157050625298', '88150.809749-0 88150.809748-1', '1.8876775693366525', '-20.312302916953133', '-288.5078002264259', '20.0', '15.0', '-20.0', '0.066337739085217', '0.02383990799003523', '0.9975123874719999', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15178084031019365', '0.23367889243106407', '0.9603940606578754'], ['88150.81287299999', '2', '0.8563407607235008', '0.49480891967227525', '0.40212548107788093', '88150.809749-0 88150.815997-1', '1.9143205570488036', '-20.302479515303915', '-288.10244511278336', '20.0', '15.0', '-20.0', '0.066337739085217', '0.02383990799003523', '0.9975123874719999', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15209387168484892', '0.23398036211890086', '0.960271130638957'], ['88150.814136', '2', '0.7163977592425953', '0.4940573082286851', '0.4012330563625609', '88150.81227499999-0 88150.815997-1', '2.2056744751225352', '-20.62017531944198', '-289.9667776826677', '20.0', '15.0', '-20.0', '0.06482411283239349', '0.024374864353150883', '0.9975989677136097', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15209387168484892', '0.23398036211890086', '0.960271130638957'], ['88150.8159975', '2', '0.8361660611207393', '0.49362903233597083', '0.40058294061730537', '88150.81599799999-0 88150.815997-1', '2.3733773877048456', '-20.832665442340023', '-291.0391610701988', '20.0', '15.0', '-20.0', '0.06396210553253093', '0.024893336791321347', '0.9976418048775022', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15209387168484892', '0.23398036211890086', '0.960271130638957'], ['88150.818051', '2', '0.8361660611207393', '0.49377224367345585', '0.40027023387364546', '88150.81599799999-0 88150.820104-1', '2.3258154849723436', '-20.950335079207825', '-291.7653248393765', '20.0', '15.0', '-20.0', '0.06396210553253093', '0.024893336791321347', '0.9976418048775022', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1515273956574198', '0.23414301276977342', '0.9603210389949675'], ['88150.821819', '2', '0.861112802026488', '0.49345299232127626', '0.3998385355256633', '88150.823534-0 88150.820104-1', '2.4518232083297207', '-21.099370287407083', '-292.5744058209181', '20.0', '15.0', '-20.0', '0.06331981940889361', '0.025199443737810433', '0.9976750916532551', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1515273956574198', '0.23414301276977342', '0.9603210389949675']]\n"
     ]
    }
   ],
   "source": [
    "data=[]#存储采样率为100hz的数据\n",
    "samp_count={}\n",
    "for i in samp.keys():\n",
    "    samp_count[i]=0\n",
    "#通过字典记录了秒数\n",
    "with open(\"gaze.csv\") as fp:\n",
    "    reader = csv.DictReader(fp)\n",
    "    for i in reader:\n",
    "        if samp_count[i[\"gaze_timestamp\"].split('.')[0]]<=100:\n",
    "            data.append(list(i.values()))\n",
    "            samp_count[i[\"gaze_timestamp\"].split('.')[0]]+=1\n",
    "        else:\n",
    "            pass\n",
    "print(data[:20])#输出前20行数据"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
